package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {

    private PermissionMapper permissionMapper;

    @Autowired
    public void setPermissionMapper(PermissionMapper permissionMapper) {
        this.permissionMapper = permissionMapper;
    }

    @Override
    public void save(Permission permission) {
        permissionMapper.insert(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        //调用分页插件方法,准备分页
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()) ;//当前页,每页显示数量
        //调用自己写的sql,不需要写limit
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo<Permission>(permissions);
    }

    //spring上下文对象
    @Autowired
    private ApplicationContext ctx;

    //项目启动时候,处理器映射器会收集所有的映射路径以及对应的处理方法
    @Autowired
    private RequestMappingHandlerMapping handlerMapping;

    @Override
    public void reload() {
        //获取数据库中所有权限表达式
        List<String> permissions = permissionMapper.selectAllExpression();
        //利用springmvc的处理器映射器来获取所有请求方法
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = handlerMapping.getHandlerMethods();
        Collection<HandlerMethod> values = handlerMethods.values();
        for (HandlerMethod m : values) {
            RequiredPermission methodAnnotation = m.getMethodAnnotation(RequiredPermission.class);
            //判断是否有注解
            if(methodAnnotation==null){
                continue; //跳过
            }
            //从注解中获取权限名称和表达式
            String name = methodAnnotation.name();
            String expression = methodAnnotation.expression();
            //判断当前方法的权限表达式是否已经存在
            if(!permissions.contains(expression)){
                //封装权限对象,并保存到数据库
                Permission permission = new Permission();
                permission.setName(name);
                permission.setExpression(expression);
                permissionMapper.insert(permission);
                //插入数据后把数据存到集合中
                permissions.add(expression);
            }
        }

       /* //获取数据库中所有权限表达式
        List<String> permissions = permissionMapper.selectAllExpression();
        //从spring容器中获取到所有的控制器对象
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        //获取map中的所有value
        Collection<Object> beans = map.values();
        for (Object bean : beans) {
            //获取控制器的字节码对象
            Class<?> aClass = bean.getClass();
            //通过反射获取该字节码中的每个方法
            Method[] methods = aClass.getDeclaredMethods();
            for (Method method : methods) {
                //从方法上获取自定义注解
                RequiredPermission annotation = method.getAnnotation(RequiredPermission.class);
                //判断是否有注解
                if(annotation==null){
                    continue; //跳过
                }
                //从注解中获取权限名称和表达式
                String name = annotation.name();
                String expression = annotation.expression();
                //判断当前方法的权限表达式是否已经存在
                if(!permissions.contains(expression)){
                    //封装权限对象,并保存到数据库
                    Permission permission = new Permission();
                    permission.setName(name);
                    permission.setExpression(expression);
                    permissionMapper.insert(permission);
                    //插入数据后把数据存到集合中
                    permissions.add(expression);
                }
            }
        }*/
    }

    @Override
    public List<Permission> selectByRoleId(Long id) {
        return permissionMapper.selectByRoleId(id);
    }

    @Override
    public List<String> selectByEmpId(Long id) {
        return permissionMapper.selectByEmpId(id);
    }
}
